package com.su.kddq.controller.courier;

import com.su.kddq.entity.user.ApplyCourier;
import com.su.kddq.service.courier.CourierStatusService;
import com.su.kddq.service.user.ApplyCourierService;
import com.su.kddq.service.user.OrderService;
import com.su.kddq.service.user.UserService;
import com.su.kddq.utils.result.R;
import com.su.kddq.utils.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 取件员controller
 *
 * @author sx
 */
@RestController
@RequestMapping("/hddq/express/h5/user/courier/")
public class CourierController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @Autowired
    private CourierStatusService courierStatusService;

    @Autowired
    private ApplyCourierService applyCourierService;


    /**
     * 取件员抢单业务
     *
     * @param id
     * @return
     */
    @PutMapping({"vie/{id}"})
    @PreAuthorize("hasAuthority(2)")
    public R orderGrabbing(@PathVariable String id) {
        boolean b = orderService.orderGrabbing(id);
        return b ? R.ok() : R.error().code(ResultCode.FAIL_QD);
    }


    /**
     * 取件员代送订单列表
     * 取件员权限校验
     *
     * @return
     */
    @GetMapping({"order/list"})
    @PreAuthorize("hasAuthority(2)")
    public R getPendingOrder(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        Map map = orderService.getPendingOrder(pageNum, pageSize);
        return R.ok().data("map", map);
    }

    /**
     * 修改订单状态
     * cid消息推送
     *
     * @param id
     * @return
     */
    @PutMapping({"state/{id}"})
    @PreAuthorize("hasAuthority(2)")
    public R updateOrderStatus(@PathVariable String id) {
        orderService.updateOrderStatus(id);
        return R.ok();
    }

    /**
     * 用户申请取件员
     *
     * @param applyCourier
     * @return
     */
    @PostMapping({"applycourier"})
    public R applyCourier(@RequestBody ApplyCourier applyCourier) {
        boolean isApply = applyCourierService.applyCourier(applyCourier);
        return isApply ? R.ok() : R.error();
    }


    /**
     * 修改取件员在线，下线状态
     * 没有待送订单-修改成功
     *
     * @param map
     * @return
     */
    @PostMapping({"accept"})
    @PreAuthorize("hasAuthority(2)")
    public R changeMyStatus(@RequestBody Map map) {
        Map isChangeMap = userService.changeMyStatus(map);
        boolean isChange = (Boolean) isChangeMap.get("isChange");
        return isChange ? R.ok() : R.error().code((Integer) isChangeMap.get("code"));
    }

    /**
     * 获取申请取件员记录，判断是否已申请业务
     *
     * @return
     */
    @GetMapping({"getapplyInfo"})
    public R getApplyInfo() {
        String id = SecurityContextHolder.getContext().getAuthentication().getName();
        ApplyCourier applyCourier = applyCourierService.getApplyInfo(id);
        return R.ok().data("applyCourier", applyCourier);
    }

    /**
     * 获取取件员当天和本月收入以及总收入
     * 结算订单状态已经完成5
     *
     * @return
     */
    @GetMapping({"getMoneybyDayAndMonth"})
    @PreAuthorize("hasAuthority(2)")
    public R getMoneyByDayAndMonth() {
        Map moneyMap = courierStatusService.getMoneybyDayAndMonth();
        return R.ok().data("moneyMap", moneyMap);
    }

    /**
     * 历史订单收益
     *
     * @return
     */
    @GetMapping("getMoneybyMonthList")
    @PreAuthorize("hasAuthority(2)")
    public R getMoneybyMonthList(@RequestParam(value = "pageNum") Integer pageNum,
                                 @RequestParam(value = "pageSize") Integer pageSize) {
        Map map = orderService.getMoneybyMonthList(pageNum, pageSize);
        return R.ok().data("map", map);
    }

    /**
     * 收益提现
     *
     * @return
     */
    @PostMapping("moneytx")
    public R MoneyTx() {
        boolean isTx = courierStatusService.MoneyTx();
        return isTx ? R.ok() : R.error();
    }

    /**
     * 查询提现状态
     * @return
     */
    @GetMapping("querytxstate")
    public R queryTxState(){
       Integer txState= courierStatusService.queryTxState();
       return R.ok().data("txState",txState);
    }

}
